<?php

/**
 * 收货地区
 */
class omeauto_auto_type_address extends omeauto_auto_type_abstract implements omeauto_auto_type_interface {

    /**
     * 在显示前为模板做一些数据准备工作
     *
     * @param object $tpl
     * @return void
     */
    public function _prepareUI(& $tpl) {
        //编辑状态获取selected_str
        if($tpl->pagedata["init"]["content"]){
            //获取“已选择区域”的字符串及value
            $regionsObj = app::get('eccommon')->model('regions');
            $tpl->pagedata['sel_regions_list'] = $regionsObj->getList('region_id,local_name',array('region_id'=>$tpl->pagedata["init"]["content"]));
            foreach ($tpl->pagedata['sel_regions_list'] as $var_key=>&$var_sel){
                if(intval($var_key)>0){
                    $var_sel['comma'] = true;
                }
            }
            unset($var_sel);
            //获取选中的region_id的字符串
            $tpl->pagedata["selected_str"] = implode(",", $tpl->pagedata["init"]["content"]);
        }
        //新增和编辑都需获取delete icon的path
        $tpl->pagedata["delete_img_src"] = app::get('desktop')->res_url.'/bundle/delete.gif'; 
    }

    /**
     * 检查输入的参数
     *
     * @param Array $params
     * @returm mixed
     */
    public function checkParams($params) {

        if (!$params['address']) {
            return "你还没有选择地区，现在“已选择地区”为空\n\n请通过下拉框筛选地区列表，并点击所需地区加入到已选择地区处后再试！！";
        }

        return true;
    }

    /**
     * 生成规则字串
     *
     * @param Array $params
     * @return String
     */
    public function roleToString($params) {

        $arr_address = explode(',',$params['address']);
        
        $rows = kernel::single('eccommon_regions')->getList('local_name', array('region_id' => $arr_address));

        $caption = '';
        foreach ($rows as $row) {

            $caption .= ", " . $row['local_name'];
        }
        $caption = sprintf('收货区域在 %s ', preg_replace('/^,/is', '', $caption));
        
        $role = array('role' => 'address', 'caption' => $caption, 'content' => $arr_address);

        return json_encode($role);
    }

    /**
     * 设置已经创建好的配置内容
     *
     * @param array $params
     * @return void
     */
    public function setRole($params) {
        $this->content = array();
        if (!empty($params)) {
            $this->content = $params;
        }
    }

    /**
     * 检查订单数据是否符合要求
     *
     * @param omeauto_auto_group_item $item
     * @return boolean
     */
    public function vaild($item) {
        if (!empty($this->content)) {
            $regionLib = kernel::single('eccommon_regions');
            foreach ($item->getOrders() as $order) {
                //获取订单的当前收获地址region_id
                $area = $order['ship_area'];
                if(!$area){
                    return false;
                }
                $arr_area = explode(":",$area);
                $region_id = end($arr_area);
                if(!$region_id || !is_numeric($region_id)){
                    return false;
                }
                $rs_region = $regionLib->getOneById($region_id);
                $arr_region_path = explode(",",$rs_region['region_path']);
                $rel_region_path = array();
                foreach ($arr_region_path as $var_path){
                    if($var_path){
                        $rel_region_path[] = $var_path;
                    }
                }
                //如region_path与收获地址规则没有交集 则说明不符合此规则
                if(!array_intersect($rel_region_path,$this->content)){
                    return false;
                }
            }
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取地区列表
     *
     * @param Array $regionLists
     * @param Integer $parent
     * @return Array
     */
    private function _getRegions($regionLists, $parent= 0) {

        $ret = array();

        foreach ($regionLists as $row) {

            $path = ereg_replace('^,|,$', '', $row['region_path']);
            $depth = substr_count($path, ',');
            if ($depth == 0) {

                $ret[$path]['caption'] = $row['local_name'];
            } elseif ($depth == 1) {

                $path = split(',', $path);
                $ret[$path[0]]['items'][$path[1]]['caption'] = $row['local_name'];
            } else {

                $path = split(',', $path);
                $ret[$path[0]]['items'][$path[1]]['items'][$path[2]] = $row['local_name'];
            }
        }

        return $ret;
    }

}